package com.example.demo.base;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Transactional(readOnly = true, rollbackFor = Exception.class)
public class BaseService<T extends BaseEntity, K extends BaseDao<T>> {

    @Autowired
    private K dao;

    /**
     * 查询实体信息
     * @param id
     * @return
     */
    public T get(String id) {
        return dao.get(id);
    }

    /**
     * 查询实体信息
     * @param entity
     * @return
     */
    public T get(T entity) {
        return dao.get(entity);
    }

    /**
     * 查询集合
     * @param entity
     * @return
     */
    public List<T> findList(T entity) {
        return dao.findList(entity);
    }

    /**
     * 新增和修改
     * @param entity
     */
    @Transactional(readOnly = false,rollbackFor = Exception.class)
    public void save(T entity) {
        if (!StringUtils.isNotBlank(entity.getId())) {
            entity.preInit();
            dao.insert(entity);
        } else {
            entity.preInit();
            dao.update(entity);
        }
    }

    /**
     * 逻辑删除
     * @param id
     * @return
     */
    @Transactional(readOnly = false,rollbackFor = Exception.class)
    public Integer logicalDeletion(String id){
        return dao.logicalDeletion(id);
    }

    /**
     * 实际删除
     */
    @Transactional(readOnly = false,rollbackFor = Exception.class)
    public Integer actualDeletion(String id){
        return dao.actualDeletion(id);
    }

}
